<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/4
 * Time: 13:51
 */

namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\Db;

class BannerCategory extends AdminBase
{
    public function index()
    {
        $chk = check_juri(request()->controller(), 'index', $this->_juri);
        if ($chk['status'])
        {
            $list = Db::name('banner_category')
                ->field('id, title, create_time, update_time')
                ->order('create_time desc')
                ->select();
            return view('index', [
                'list'=>$list
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function add()
    {
        $chk = check_juri(request()->controller(), 'add', $this->_juri);
        if ($chk['status'])
        {
            return view();
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function doAdd()
    {
        if (request()->isPost())
        {
            $data                = [];
            $data['title']       = input('param.title');
            $data['create_time'] = time();
            $data['update_time'] = $data['create_time'];
            $res = Db::name('banner_category')->insertGetId($data);
            if ($res)
            {
                return true;
            } else {
                return false;
            }
        }
    }

    public function edit()
    {
        $chk = check_juri(request()->controller(), 'edit', $this->_juri);
        if ($chk['status'])
        {
            $id = intval(input('param.id'));
            $info = Db::name('banner_category')
                ->field('id, title')
                ->where('id', 'eq', $id)
                ->find();
            $this->assign('info', $info);
            return view('edit', [
                'info'=>$info
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function doEdit()
    {
        if (request()->isPost())
        {
            $id                  = intval(input('param.id'));
            $data                = [];
            $data['title']       = input('param.title');
            $data['update_time'] = time();
            $res = Db::name('banner_category')->where('id', 'eq', $id)->update($data);
            if ($res)
            {
                return true;
            } else {
                return false;
            }
        }
    }

    public function del()
    {
        if (request()->isPost())
        {
            $chk = check_juri(request()->controller(), 'del', $this->_juri);
            if ($chk['status'])
            {
                $id   = intval(input('param.id'));
                $list = Db::name('banner')
                    ->field('thumb')
                    ->where('banner_category_id', 'eq',$id)
                    ->select();
                $res = Db::name('banner_category')->where(['id'=>$id])->delete();
                if ($res)
                {
                    Db::name('banner')->where('banner_category_id', 'eq', $id)->delete();
                    if (!empty($list))
                    {
                        foreach ($list as $val) {
                            if (!empty($val['thumb']))
                            {
                                $path = './public/'.$val['thumb'];
                                if (file_exists($path))
                                {
                                    @unlink($path);
                                }
                            }
                        }
                    }
                    return ['status'=>true, 'msg'=>'删除成功'];
                } else {
                    return ['status'=>false, 'msg'=>'删除失败'];
                }
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }

        }
    }
}